﻿//-----------------------------------------------------------------------
// <copyright file="Common.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation 2011.
// All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Yogesh M Joshi</author>
// <date>02-10-2011</date>
// <summary></summary>
//-----------------------------------------------------------------------

namespace SPActivityFeed.Utility
{
    using System;
    using System.Globalization;
    using System.IO;
    using System.Reflection;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml;
    using System.Xml.Linq;
    using System.Xml.Xsl;
    using Microsoft.SharePoint.WebControls;

    public static class Common
    {
        /// <summary>
        /// Registers the given Style sheet
        /// </summary>       
        public static void RegisterWebResource(Page page, Type type, string resourceId)
        {
            if (page != null)
            {
                CssRegistration.Register(page.ClientScript.GetWebResourceUrl(type, resourceId));
            }
        }

        /// <summary>
        /// Registers the given js file
        /// </summary>
        /// <param name="page">The current Page instance.</param>
        /// <param name="resourceName">Resource Name of Js file.</param>
        public static void RegisterScriptResource(Page page, Type type, string resourceId)
        {
            if (page != null)
            {
                page.ClientScript.RegisterClientScriptResource(type, resourceId);
            }
        }

        public static void RenderHtml(TextWriter writer, XElement element, string strXslt, string resourceId)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            if (element == null)
            {
                throw new ArgumentNullException("xmlBlob", "XElement xmlBlob cannot be null.");
            }

            if (String.IsNullOrEmpty(strXslt))
            {
                strXslt = GetResourceAsString(resourceId);
            }

            using (System.Xml.XmlTextReader strReader = new System.Xml.XmlTextReader(strXslt, XmlNodeType.Document, null))
            {
                XslCompiledTransform myXslTrans = new XslCompiledTransform();
                myXslTrans.Load(strReader);
                XsltArgumentList argsList = new XsltArgumentList();
                XmlDocument results = new XmlDocument();
                results.LoadXml(element.ToString());
                myXslTrans.Transform(results, argsList, writer);
            }
        }

        public static Control GetWaitingImage()
        {
            Image img = new Image()
            {
                ImageUrl = "/_layouts/IMAGES/hig_progcircle_loading24.gif",
                Width = 24,
                Height = 24
            };

            img.Style.Add("display", "block");
            img.Style.Add("margin-left", "auto");
            img.Style.Add("margin-right", "auto");
            img.Style.Add("margin-top", "10px");
            img.Style.Add("margin-bottom", "10px");

            return img;
        }

        private static string GetResourceAsString(string resourceId)
        {
            Stream resStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceId);

            if (resStream == null)
            {
                throw new FileNotFoundException(String.Format(CultureInfo.InvariantCulture, "Embedded Resource, {0}, does not exist.", resourceId));
            }

            byte[] streamBytes = new byte[resStream.Length];
            resStream.Position = 0;
            resStream.Read(streamBytes, 0, (int)resStream.Length);
            string result = Encoding.UTF8.GetString(streamBytes);
            return result.Trim();
        }
    }
}
